package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.linkage.educloud.ah.R;

/**
 * <desc>
 * Created by The Moss on 2018/8/27.
 */

public class LoginRoleAdapter extends BaseAdapter {
    Context mContext;
    int checked = 0;
    ItemClickListener listener;

    public interface ItemClickListener {
        void onItemClick(int position);
    }

    public LoginRoleAdapter(Context context, ItemClickListener listener) {
        this.mContext = context;
        this.listener = listener;
    }

    public void setChecked(int checked) {
        if (this.checked == checked) {
            return;
        }
        this.checked = checked;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public String getItem(int position) {
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_adapter_dialog_role, null);
            holder = new ViewHolder();
            holder.imageView = convertView.findViewById(R.id.item_dialog_role_image);
            holder.textView = convertView.findViewById(R.id.item_dialog_role_text);
            holder.radioButton = convertView.findViewById(R.id.item_dialog_role_radio);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            holder.imageView.setImageResource(R.mipmap.ic_dialog_teacher_tv);
            holder.textView.setText("教师");
        }
        if (position == 1) {
            holder.imageView.setImageResource(R.mipmap.ic_dialog_parent_tv);
            holder.textView.setText("家长");
        }
        if (checked == position) {
            holder.radioButton.setChecked(true);
        } else {
            holder.radioButton.setChecked(false);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    setChecked(position);
                    listener.onItemClick(position);
                }
            }
        });
        return convertView;
    }

    private class ViewHolder {
        ImageView imageView;
        TextView textView;
        RadioButton radioButton;
    }
}
